<?php

/**
 * ClassName:
 * @auditor LeiJunXiang
 * @time 2024/12/05
 */

namespace app\controller\onlineChat;

use app\BaseController;
use app\factory\Factory;
use app\factory\model\ResponseAgreement;
use app\modelOpreate\onlineChat\HistoryRecordOpe;
use app\tool\onlineChat\MsgListSelect;
use think\facade\Cache;

class DialogBoxController extends BaseController
{
    /**
     * 消息列表
     */
    public function msgList($userId)
    {
        $tool = Factory::getInstance()->getToolObj(MsgListSelect::TOOL_MSG_LIST_SELECT);
        //(消息列表)开始查询(用户id)
        $msgList = $tool->startSelect($userId);
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
        if ($msgList) {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
            $responseObj->setContent($msgList);
        } else {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg($tool->getErrMsg());
        }
    }

    /**
     * 关闭socket接口
     * @return void
     */
    public function closeSocket($userId)
    {
        $flag = false;
        if($userId){
            $redis = Cache::store('redis');
            $flag = $redis->delete($userId . "_WebSocket");
        }
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
        if ($flag) {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
        }else{
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("关闭失败");
        }
    }

    /**
     * 历史记录(房间号,页数,条数)
     * @return void
     */
    public function historyRecord($roomNumber, $page, $pageSize)
    {
        $toolObj = Factory::getInstance()->getToolObj(HistoryRecordOpe::TOOL_HISTORY_RECORD_OPE);
        //获取历史记录(房间号,页数,条数)
        $historyRecord = $toolObj->getHistoryRecordByRoomNumber($roomNumber, $page, $pageSize);
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
        if (empty($historyRecord)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("没找到聊天记录，快来开始聊天吧！");
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
            $responseObj->setContent($historyRecord);
        }
    }
}